#!/bin/bash

# 停止并禁用 dovecot 和 cyrus-imapd 服务
systemctl stop dovecot.service cyrus-imapd.service 2>/dev/null
systemctl disable dovecot.service cyrus-imapd.service 2>/dev/null

# 卸载所有 dovecot 和 cyrus-imapd 相关包（兼容 dnf/yum/apt）
if command -v dnf >/dev/null 2>&1; then
    dnf remove -y $(rpm -qa | grep -E '^(dovecot|cyrus-imapd)') 2>/dev/null
elif command -v yum >/dev/null 2>&1; then
    yum remove -y $(rpm -qa | grep -E '^(dovecot|cyrus-imapd)') 2>/dev/null
elif command -v apt-get >/dev/null 2>&1; then
    apt-get purge -y dovecot-core dovecot-imapd dovecot-pop3d cyrus-imapd 2>/dev/null
fi

# 验证
if rpm -qa 2>/dev/null | grep -qE '^(dovecot|cyrus-imapd)' || dpkg -l 2>/dev/null | grep -qE 'dovecot|cyrus-imapd'; then
    echo "[-] imap 或 pop3-server 包仍然存在"
    exit 1
else
    echo "[+] imap 和 pop3-server 相关包已卸载"
    exit 0
fi
